package com.cdplife.ci.modular.system.controller;

import cn.stylefeng.roses.core.base.controller.BaseController;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.cdplife.ci.core.common.annotion.BussinessLog;
import com.cdplife.ci.core.common.annotion.FormToken;
import com.cdplife.ci.core.common.annotion.Permission;
import com.cdplife.ci.core.common.constant.dictmap.PolicyDict;
import com.cdplife.ci.core.common.constant.factory.PageFactory;
import com.cdplife.ci.core.common.page.PageInfoBT;
import com.cdplife.ci.core.util.StringUtil;
import com.cdplife.ci.modular.system.model.Insurance;
import com.cdplife.ci.modular.system.service.IInsuranceService;
import com.cdplife.ci.modular.system.service.ITenantService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.cdplife.ci.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.cdplife.ci.modular.system.model.Policy;
import com.cdplife.ci.modular.system.service.IPolicyService;
import com.cdplife.ci.modular.system.warpper.PolicyWarpper;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 控制器
 *
 * @author jers
 * @Date 2018-11-07 14:27:12
 */
@Controller
@RequestMapping("/policy")
public class PolicyController extends BaseController {

    private String PREFIX = "/system/policy/";

    @Autowired
    private IPolicyService policyService;

    @Autowired
    private ITenantService tenantService;
    @Autowired
    private IInsuranceService insuranceService;


    /**
     * 跳转到首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "policy.html";
    }

    /**
     * @brief 跳转到添加
     * @param model
     * @return
     */
    @FormToken(save=true)
    @RequestMapping("/policy_add")
    public String policyAdd(Model model) {
        //获取所有保险公司
        model.addAttribute("insurances", insuranceService.selectInsurance(""));
        //获取所有客户
        model.addAttribute("tenants", tenantService.selectList(null));
        return PREFIX + "policy_add.html";
    }

    /**
     * @brief 跳转到修改
     * @param policyId
     * @param model
     * @return
     */
    @FormToken(save=true)
    @RequestMapping("/policy_update/{policyId}")
    public String policyUpdate(@PathVariable Integer policyId, Model model) {
        Policy policy = policyService.selectById(policyId);
        model.addAttribute("item",policy);
        //获取所有保险公司
        model.addAttribute("insurances", insuranceService.selectInsurance(""));
        //获取所有客户
        model.addAttribute("tenants", tenantService.selectList(null));
        LogObjectHolder.me().set(policy);
        return PREFIX + "policy_edit.html";
    }

    /**
     * @brief 跳转到修改
     * @param policyId
     * @param model
     * @return
     */
    @FormToken(save=true)
    @RequestMapping("/re_new/{policyId}")
    public String policyReNew(@PathVariable Integer policyId, Model model) {
        Policy policy = policyService.selectById(policyId);
        String defaultDay = StringUtil.getSpecifiedDayAfter(policy.getInvalidDate());
        model.addAttribute("item",policy);
        model.addAttribute("defaultDay",defaultDay);
        //获取所有保险公司
        Wrapper<Insurance> wrapper = new EntityWrapper<Insurance>();
        wrapper = wrapper.eq("is_del", 0);
        model.addAttribute("insurances", insuranceService.selectList(wrapper));
        LogObjectHolder.me().set(policy);
        return PREFIX + "policy_re_new.html";
    }

    /**
     * @brief 跳转到添加联系人
     * @param policyId
     * @param model
     * @return
     */
    @FormToken(save=true)
    @RequestMapping("/contacts_add/{policyId}")
    public String policyContactsAdd(@PathVariable Integer policyId, Model model) {
        Policy policy = policyService.selectById(policyId);
        model.addAttribute("item",policy);
        return PREFIX + "contacts_add.html";
    }

    /**
     * @brief 获取列表
     * @param policyCode 搜索条件 保单编号
     * @param policyStauts 搜索条件 保单状态
     * @return
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam(required = false) String policyCode,@RequestParam(required = false) Integer policyStauts) {
        Page<Policy> page = new PageFactory<Policy>().defaultPage();
        List<Map<String, Object>> result = policyService.getPolicyList(page, policyCode, policyStauts, page.getOrderByField(), page.isAsc());
        page.setRecords(new PolicyWarpper(result).wrap());
        return new PageInfoBT<>(page);
    }

    /**
     * @brief 新增保单
     * @param policy
     * @return
     */
    @FormToken(remove=true)
    @Permission
    @BussinessLog(value = "添加保单", key = "policyCode", dict = PolicyDict.class)
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Policy policy) {
        return policyService.addPolicy(policy);
    }

    /**
     * @brief 删除保单
     * @param policyId
     * @return
     */
    @Permission
    @BussinessLog(value = "删除保单", key = "policyId", dict = PolicyDict.class)
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer policyId) {
        return policyService.deletePolicy(policyId);
    }

    /**
     * 修改
     */
    @FormToken(remove=true)
    @Permission
    @BussinessLog(value = "修改保单", key = "policyCode", dict = PolicyDict.class)
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Policy policy) {
        return policyService.updatePolicy(policy);
    }

    /**
     * 续约保单
     */
    @FormToken(remove=true)
    @Permission
    @BussinessLog(value = "续约保单", key = "policyCode", dict = PolicyDict.class)
    @RequestMapping(value = "/reNew")
    @ResponseBody
    public Object reNew(Policy policy) {
        return policyService.reNew(policy);
    }

    /**
     * 修改联系人
     */
    @FormToken(remove=true)
    @Permission
    @BussinessLog(value = "修改保单联系人", key = "policyCode", dict = PolicyDict.class)
    @RequestMapping(value = "/contacts")
    @ResponseBody
    public Object contacts(Policy policy) {
        return policyService.updateContacts(policy);
    }

    /**
     * 查看保单详情
     */
    @RequestMapping(value = "/detail/{policyId}")
    @ResponseBody
    public Object detail(@PathVariable("policyId") Integer policyId) {
        return policyService.selectById(policyId);
    }

}
